import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:good_good_study/common/index.dart';
import 'package:good_good_study/provider/collect_provider.dart';
import 'package:good_good_study/provider/home_provider.dart';
import 'package:provider/provider.dart';
import 'package:good_good_study/entity/home_list_entity.dart' as Article;

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> with NetCancel {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: SizedBox(),
        title: Text("首页"),
        centerTitle: true,
      ),
      body: Consumer<HomeProvider>(
        builder: (context, articleProvider, child) {
          if (articleProvider.nextPageIndex == 0) {
            articleProvider.getListPage(this, page: 0);
          }
          return articleProvider.articleList.isEmpty
              ? GestureDetector(
                  onTap: () {
                    articleProvider.getListPage(this, page: 0);
                  },
                  child: buildPageEmpty(),
                )
              : Container(
                  child: EasyRefresh(
                    header: MaterialHeader(),
                    footer: MaterialFooter(),
                    onRefresh: () {
                      articleProvider.getListPage(this, page: 0);
                      return Future.value(null);
                    },
                    onLoad: () {
                      articleProvider.getListPage(this);
                      return Future.value(null);
                    },
                    child: ListView.builder(
                      itemBuilder: (context, pos) {
                        return buildListItem(
                            context, pos, articleProvider.articleList[pos]);
                      },
                      itemCount: articleProvider.articleList.length,
                    ),
                  ),
                );
        },
      ),
    );
  }

  Widget buildListItem(BuildContext context, int pos, Article.Datas item) {
    return GestureDetector(
      onTap: () {
        RouteUtil.toWebView(context, item.title, item.link);
      },
      child: Container(
        alignment: Alignment.center,
        color: Color(0x123456 * pos * 100),
        height: 60,
        child: Row(
          children: [
            IconButton(
              icon: Icon(
                item.collect ? Icons.favorite : Icons.favorite_border,
                color: Colors.red,
              ),
              onPressed: () {
                var homeProvider = context.read<HomeProvider>();
                var collectProvider = context.read<CollectProvider>();
                if (item.collect) {
                  collectProvider.cancelCollect(this, item.id, () {
                    item.setCollect(false);
                    homeProvider.updateItem(pos, item);
                  });
                } else {
                  collectProvider.doCollect(this, item.id, () {
                    item.setCollect(true);
                    homeProvider.updateItem(pos, item);
                  });
                }
              },
            ),
            Expanded(
              child: Text(item.title),
            )
          ],
        ),
      ),
    );
  }
}
